//开发版
var gulp = require("gulp");
var gulp_min_css = require("gulp-minify-css");
var gulp_sass = require("gulp-sass");
var gulp_plumber = require("gulp-plumber");
var gulp_tinypng = require("gulp-tinypng");
var gulp_uglify = require("gulp-uglify");
var browser_sync = require("browser-sync").create();
var gulp_copy = require("gulp-copy");
var gulp_autoprefixer = require("gulp-autoprefixer");
//压缩代码文件
gulp.task("mincss",function(){
	gulp.src('src/scss/**/*.scss')
	    .pipe(gulp_plumber())
	    .pipe(gulp_sass())
	    .pipe(gulp_min_css({
	    	keepSpecialComments: '*' //保留所有特殊前缀 当你用autoprefixer生成的浏览器前缀，如果不加这个参数，有可能将会删除你的部分前缀
	    }))
	    .pipe(gulp_autoprefixer({
	    	browsers: ['last 2 versions', 'Android >= 4.0','iOS 7','last 3 Safari versions'],
			cascade: false
	    }))
	    .pipe(gulp.dest('src/css'));
});





//实时监听scss文件，并转化
gulp.task("watchcss",function(){
	gulp.watch('src/scss/**/*.scss',["mincss"]);
})


gulp.task("minimg",function(){
	gulp.src('src/img/**/*.*')
	    .pipe(gulp_plumber())
	    .pipe(gulp_tinypng("F130o5pY96-2AezmpEp8EuYgyxXD0ufD"))
	    .pipe(gulp.dest('./dist/img'));
});



//定义自动刷新浏览器任务
gulp.task("browser-sync",function(){
	browser_sync.init({
		port:Math.floor(Math.random()*9999)+1000,//产生随机端口
		server: {
			baseDir:"./src"
		}
	});
	gulp.watch("src/scss/**/*.scss",["mincss"]).on("change",browser_sync.reload);
	gulp.watch("src/*.html").on("change",browser_sync.reload);
})
gulp.task("dev",["browser-sync"]);

//最终完成版
//定义js
gulp.task("minjs",function(){
	gulp.src('src/js/**/*.js')
	    .pipe(gulp_uglify())
	    .pipe(gulp.dest('dist/js'));
});


gulp.task("copy",function(){
//	 gulp.src("src/img/**/*").pipe(gulp_copy("dist",{prefix:"1"}))//复制所有图片
     gulp.src("src/js/**/*").pipe(gulp_copy("dist",{prefix:"1"}))//复制所有JS文件    
     gulp.src("src/css/**/*").pipe(gulp_copy("dist",{prefix:"1"}))//复制所有CSS文件
     gulp.src("src/*html").pipe(gulp_copy("dist",{prefix:"1"}))//复制所有的html文件
     gulp.src("src/scss/**/*.scss").pipe(gulp_copy("dist",{prefix:"1"}))
     gulp.src("src/fonts/**/*.*").pipe(gulp_copy("dist",{prefix:"1"}))
});

//一键生成最终完成版
gulp.task("pro",["minjs","copy","minimg"]);
